﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using cbmobile.BLC;
using cbmobile.DAC;
using PQT.API;
using PQT.API.File;
using PQT.Common;
using PQT.Controls;
using Telerik.WebControls;
using PQT.API.DataDefine.Sys;



namespace cbmobile.MasterUser.UC_Page
{
    public partial class ProdInfo_UC : System.Web.UI.UserControl
    {
        BLC_SanPham blc_Sanpham = new BLC_SanPham();
        
        protected void Page_Load(object sender, EventArgs e)
        {   
            if (!IsPostBack)
            {
                if (Request.QueryString["MA_SP"] != null)
                {
                    string strma_sp = Request.QueryString["MA_SP"].ToLower();
                    int ma_sp = int.Parse(strma_sp);
                    LoadProd(ma_sp);
                }
            }
        }

        private void LoadProd(int ma_SP)
        {
            //Sơ lượt---------------------------------------
            SanPham sp = blc_Sanpham.Get_SanPham_byId(ma_SP);
            lbl_Name.Text = sp.Ten_SP.ToString();
            lblright_top.Text = sp.Ten_SP.ToString();
            imgSP.ImageUrl = "~/ImagesSP/" + sp.HinhAnh.ToString();
            lblKichThuoc.Text = sp.KichThuoc.ToString();
            lblTrongLuong.Text = sp.TrongLuong.ToString();
            lblHeDieuHanh.Text = sp.HeDieuHanh.ToString();

            lblCPU.Text = sp.CPU.ToString();
            lblBoNhoTrong.Text = sp.OCung.ToString();
            lbl3g.Text = sp.KetNoi3G.ToString();
            lblWifi.Text = sp.Wifi.ToString();
            lblCamera.Text = sp.Camera.ToString();
            lblTinhTrang.Text = sp.TinhTrang.ToString();
            lblBaoHanh.Text = sp.BaoHanh.ToString();
            lblHangCongTy.Text = sp.HangCongTy.ToString();
            lblLuotXem.Text = sp.Count_View.ToString();
            //hlMuaHangTuXa.NavigateUrl=
            //hlDieuKhoanBaoHanh.NavigateUrl=

            //Thông số kỹ thuật--------------------------------
            lblMang2G.Text = "GSM 900 / 1800 / 1900";
            lblMang3G.Text = sp.KetNoi3G.ToString();
            lblMang3G_2.Text = sp.Support_3G.ToString();
            //lblRaMat.Text = sp.ThongSoKyThuat.ToString();
            lblMangKhongDay.Text = sp.Wifi.ToString();
            if (sp.TinhTrang == 1)
                lblTinhTrang_2.Text = "Có hàng";
            else
                lblTinhTrang_2.Text = "Đã hết hàng";
            lblKichThuoc_2.Text = sp.KichThuoc.ToString();
            lblTrongLuong_2.Text = sp.TrongLuong.ToString();
            lblHDH.Text = sp.HeDieuHanh.ToString();
            lblTocDoXuLy.Text = sp.CPU.ToString();
            lblLoaNgoai.Text = "Có";
            lbljack.Text = "Có";
            lblDanhBa.Text = "Không giới hạn các mục và fields, danh bạ";
            lblMau.Text = sp.MauSac;
            //lblKieuChuong.Text = sp.ThongSoKyThuat.ToString();
            lblBluetooth.Text = "Có, v4.0 với A2DP";
            lblGPRS.Text = "Có";
            
            lblBaoHanh.Text = sp.BaoHanh.ToString();
            lblCamera_2.Text = sp.Camera.ToString();
            decimal giacty = Convert.ToDecimal(sp.GiaCongTy.ToString());
            lblGiaCty.Text = giacty.ToString();
            lblGiaTT.Text = sp.GiaThiTruong.ToString();
            lblPhuKien.Text = "Phụ kiện của " + sp.Ten_SP.ToString() + " bao gồm";
            lblPhuKien_ND.Text = sp.PhuKien.ToString();

            lblHoTro.Text = sp.ThongSoKyThuat;

        }

        protected void cmdMuaHang_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (lblTinhTrang.Text == "1")
                {
                    string ma_sp = Request.QueryString["Ma_SP"];
                    string hinh_sp = imgSP.ImageUrl;
                    string ten_sp = lbl_Name.Text;
                    string mau_sp = lblMau.Text;
                    
                    decimal gia_sp = Convert.ToDecimal(lblGiaCty.Text);
                    int soluong = 1;
                    decimal tt = soluong * gia_sp;
                    ThemVaoGioHang(ma_sp, soluong, gia_sp, mau_sp, hinh_sp, ten_sp, tt);
                    if (Session["slsp"] == null)
                    {
                        Session["slsp"] = 1;
                    }
                    else
                    {
                        int slsp = Convert.ToInt32(Session["slsp"].ToString());
                        slsp += 1;
                        Session["slsp"] = slsp;
                    }
                    Response.Redirect("ChiTietGioHang.aspx");
                }

            }
        }

        private void ThemVaoGioHang(string masp, int soluong, decimal gia, string mau, string hinh, string tensp, decimal thanhtien)
        {
            DataTable giohang;
            if (Session["addGioHang"] == null)
            {
                giohang = new DataTable();
                giohang.Columns.Add("MaSP");
                giohang.Columns.Add("Hinh");
                giohang.Columns.Add("TenSP");
                giohang.Columns.Add("MauSac");
                giohang.Columns.Add("Gia");
                giohang.Columns.Add("SoLuong");
                giohang.Columns.Add("ThanhTien");
                giohang.Columns.Add("LoaiSP");
            }
            else
            {
                giohang = (DataTable)Session["addGioHang"];
            }
            int indexofitem = IsExistItemInShoppingCart(masp, giohang);
            if (indexofitem != -1)
            {
                giohang.Rows[indexofitem]["SoLuong"] = Convert.ToInt32(giohang.Rows[indexofitem]["SoLuong"]) + 1;
                giohang.Rows[indexofitem]["ThanhTien"] = gia * Convert.ToDecimal(giohang.Rows[indexofitem]["SoLuong"]);
            }
            else
            {
                DataRow datarow = giohang.NewRow();
                datarow["MaSP"] = masp;
                datarow["Hinh"] = hinh;
                datarow["TenSP"] = tensp;
                datarow["MauSac"] = mau;
                datarow["Gia"] = gia;
                datarow["SoLuong"] = 1;
                datarow["ThanhTien"] = gia;
                datarow["LoaiSP"]="DT";
                giohang.Rows.Add(datarow);
            }
            Session["addGioHang"] = giohang;
        }

        private int IsExistItemInShoppingCart(string itemID, DataTable dataTable)
        {
            int indexOfRow = -1;
            for (int dataRow = 0; dataRow < dataTable.Rows.Count; dataRow++)
            {
                if (dataTable.Rows[dataRow]["MaSP"].ToString() == itemID)
                {
                    indexOfRow = dataRow;
                    break;
                }
            }
            return indexOfRow;
        }
    }
    
}